<?php
namespace app\common\model;
use x\Xbase;
use x\my\Curl;
class ThreeTemp extends Xbase
{
    #获取微信access_token
    public function getWxAccessToken(){
        $temp = $this->db->query("select * from three_temp where uuid='wechat_access_token'");
        if ($temp) {
            if ($temp[0]['update_time']<time()) {
                $this->db->delete('three_temp',$temp[0]['id']);
            }else{
                return $temp[0]['value'];
            }
        }

        $time = time();
        $rs = Curl::get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".c('api.wx_xcx_appid')."&secret=".c('api.wx_xcx_appsecret'));
        $rs = json_decode($rs,true);
        if (!isset($rs['access_token'])) return $this->setError('获取微信access_token失败');

        $data = [
            'uuid' => 'wechat_access_token',
            'value' => $rs['access_token'],
            'create_time' => $time,
            'update_time' => $time+$rs['expires_in'],
        ];
        $result = $this->db->insert('three_temp',$data);
        return $rs['access_token'];
    }

}
